﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.OA.WorkflowActivity;
using BCDto.OA.WorkflowActive.WorkflowActivity;
using BCCommon.OAEnums.WorkflowActive;
using BCDto.Website.User;
using BCDto.Sim.Admin.Admin;
using BCDto.OA.WorkflowActive.WorkflowActivityStepFormConfig;
using BCDto.OA.WorkflowActive.WorkflowActivityOperation;
using BCDto.OA.WorkflowActive.WorkflowActive;
using BCDto.OA.WorkflowActive.WorkflowActivityStep;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepButtonConfig;

namespace BCTMSRESTful.Controllers.Management.OA.WorkflowActivity.WorkflowActivity
{
    /// <summary>
    /// 工作流活动
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class WorkflowActivityController : ManagementBaseController
    {
        private readonly IWorkflowActivityService workflowActivityService;

        public WorkflowActivityController(IWorkflowActivityService workflowActivityService)
        {
            this.workflowActivityService = workflowActivityService;
        }

        #region 添加工作流活动
        /// <summary>
        ///  添加工作流活动
        /// </summary>
        /// <param name="requestDto">工作流活动信息</param>
        /// <response code="201">添加工作流活动成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加工作流活动")]
        [ProducesResponseType(typeof(WorkflowActivityDto), 201)]
        public IActionResult Post([FromBody]WorkflowActivityRequestDto requestDto)
        {
            var result = this.workflowActivityService.InitActivity(requestDto.WorkflowTemplateId, this.Admin, null);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 提交工作流活动表单
        /// <summary>
        ///  提交工作流活动表单
        /// </summary>
        /// <param name="requestDtos">工作流活动步骤表单列表</param>
        /// <response code="200">修改工作流活动成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("提交工作流活动表单")]
        [ProducesResponseType(typeof(WorkflowActivityDto), 200)]
        public IActionResult SubmitForm([FromBody]List<WorkflowActivityStepFormConfigRequestDto> requestDtos)
        {
            var result = this.workflowActivityService.SubmitForm(requestDtos, this.Admin, null);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 工作流活动步骤执行操作
        /// <summary>
        /// 工作流活动步骤执行操作
        /// </summary>
        /// <param name="workflowActivityOperationRequestDto">工作流活动步骤操作信息</param>
        /// <response code="200">工作流活动步骤执行操作。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("工作流活动步骤执行操作")]
        public IActionResult StepExecution([FromBody] WorkflowActivityOperationRequestDto workflowActivityOperationRequestDto)
        {
            var result = this.workflowActivityService.Execution(workflowActivityOperationRequestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改工作流活动步骤为已激活
        /// <summary>
        ///  修改工作流活动步骤为已激活
        /// </summary>
        /// <param name="workflowActivityStepId">工作流活动步骤Id</param>
        /// <response code="200">修改工作流活动步骤状态成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{workflow_activity_step_id}")]
        [Log("修改工作流活动步骤为已激活")]
        [ProducesResponseType(typeof(WorkflowActivityStepDto), 200)]
        public IActionResult StepStatusToActivity([FromRoute(Name = "workflow_activity_step_id")] long workflowActivityStepId)
        {
            WorkflowActivityStepPutDto putDto = new WorkflowActivityStepPutDto();
            putDto.WorkflowActivityStepId = workflowActivityStepId;;
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.UpdateStepStatusToActivity(putDto,this.Admin,null));
        }
        #endregion

        #region 修改工作流活动步骤按钮显示条件
        /// <summary>
        ///  修改工作流活动步骤按钮显示条件
        /// </summary>
        /// <param name="workflowActivityStepId">工作流活动步骤Id</param>
        /// <param name="buttonShowCondition">步骤按钮显示条件</param>
        /// <response code="200">修改工作流活动步骤状态成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{workflow_activity_step_id}/{button_show_condition}")]
        [Log("修改工作流活动步骤按钮显示条件")]
        [ProducesResponseType(typeof(WorkflowActivityStepDto), 200)]
        public IActionResult StepButtonShowCondition([FromRoute(Name = "workflow_activity_step_id")] long workflowActivityStepId, [FromRoute(Name = "button_show_condition")] string buttonShowCondition)
        {
            WorkflowActivityStepButtonConditionPutDto putDto = new WorkflowActivityStepButtonConditionPutDto();
            putDto.WorkflowActivityStepId = workflowActivityStepId;
            putDto.ButtonShowCondition = buttonShowCondition;
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.UpdateStepButtonShowCondition(putDto));
        }
        #endregion

        //#region 删除工作流活动
        ///// <summary>
        /////  删除工作流活动
        ///// </summary>
        ///// <param name="workflowActivityId">工作流活动Id</param>
        ///// <response code="204">删除工作流活动成功</response>
        ///// <response code="400">请求参数或输入的数据不正确。</response>
        ///// <response code="500">服务器发生错误。</response>
        ///// <response code="501">某些功能尚未实现。</response>
        ///// <returns></returns>
        //[HttpDelete("{workflow_activity_id}")]
        //[Log("删除工作流活动")]
        //[ProducesResponseType(typeof(NoContentResult), 204)]
        //public IActionResult Delete([FromRoute(Name = "workflow_activity_id")] long workflowActivityId)
        //{
        //    this.workflowActivityService.Delete(workflowActivityId);
        //    return StatusCode((int)HttpStatusCode.NoContent);
        //}
        //#endregion

        #region 获取工作流活动
        /// <summary>
        ///  获取工作流活动
        /// </summary>
        /// <param name="workflowActivityId">工作流活动Id</param>
        /// <response code="200">获取工作流活动</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{workflow_activity_id}")]
        [ProducesResponseType(typeof(WorkflowActivityDto), 200)]
        public IActionResult Get([FromRoute(Name = "workflow_activity_id")] long workflowActivityId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.Get(workflowActivityId));
        }
        #endregion

        #region 获取工作流活动表单信息列表
        /// <summary>
        ///  获取工作流活动表单信息列表
        /// </summary>
        /// <param name="workflowActivityId">工作流活动Id</param>
        /// <response code="200">获取工作流活动表单信息列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]/{workflow_activity_id}")]
        [ProducesResponseType(typeof(IEnumerable<WorkflowActivityStepFormConfigDto>), 200)]
        public IActionResult StepForms([FromRoute(Name = "workflow_activity_id")] long workflowActivityId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.GetActivityStepFormConfigList(workflowActivityId));
        }
        #endregion

        #region 获取工作流活动步骤按钮列表（非附加步骤，附加步骤无需调用，且仅有通过和驳回）
        /// <summary>
        ///  获取工作流活动步骤按钮列表（非附加步骤，附加步骤无需调用，且仅有通过和驳回）
        /// </summary>
        /// <param name="templateStepCode">工作流模板步骤Code</param>
        /// <response code="200">获取工作流活动步骤按钮列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]/{template_step_code}")]
        [ProducesResponseType(typeof(IEnumerable<WorkflowTemplateStepButtonConfigDto>), 200)]
        public IActionResult SetpButton([FromRoute(Name = "template_step_code")] string templateStepCode)
        {
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.GetActivityStepButtonList(templateStepCode));
        }
        #endregion

        //#region 获取工作流活动列表
        ///// <summary>
        /////  获取工作流活动列表
        ///// </summary>
        ///// <response code="200">获取工作流活动列表</response>
        ///// <response code="400">请求参数或输入的数据不正确。</response>
        ///// <response code="500">服务器发生错误。</response>
        ///// <response code="501">某些功能尚未实现。</response>
        ///// <returns></returns>
        //[HttpGet]
        //[ProducesResponseType(typeof(WorkflowActivityDto), 200)]
        //public IActionResult Get()
        //{
        //    return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.qu());
        //}
        //#endregion

        #region 获取工作流活动待处理任务（分页）
        /// <summary>
        ///  获取工作流活动待处理任务（分页）
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取工作流活动（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<WorkflowActivityViewDto>), 200)]
        public IActionResult PendingHandleTaskQuery(
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.PendingHandleTaskQuery(this.Admin, start, end, sortName, ascending, index, size));
        }
        #endregion

        #region 获取工作流活动（分页）
        /// <summary>
        ///  获取工作流活动（分页）
        /// </summary>
        /// <param name="activityStatus">活动状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取工作流活动（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<WorkflowActivityDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "activity_status")] OAActivityStatus activityStatus,
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.workflowActivityService.Query(this.Admin, null, activityStatus, start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}

